package com.zhongke.cotrun.common.job;

import java.util.List;

import com.zhongke.cotrun.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.zhongke.cotrun.model.pojo.PayBankroll;
import com.zhongke.cotrun.model.pojo.PayMallInfo;
import com.zhongke.cotrun.model.pojo.PayOrder;


@Component
@EnableScheduling
public class CronJobsUtil {
	/**
	 * 日志对象
	 */
	private static final Logger logger = LoggerFactory.getLogger(CronJobsUtil.class);
	@Autowired
	private NeedInfoService needInfoService;
	@Autowired
	private PayMallInfoService payMallInfoService;
	@Autowired
	private PayOrderService payOrderService;
	@Autowired
	private PayBankrollService payBankrollService;
	@Autowired
	private InnovationActivitiesService activitiesService;
	
	@Scheduled(fixedRate = 1000 * 60)
	public void work() {
		logger.info("=============自动定时任务   开始================");		
		
		int num = needInfoService.cronJobsUpdateState3();
		logger.info("=============本次自动下线  "+num+" 条需求================");

		num = activitiesService.cronJobsUpdateState2();
		logger.info("=============本次自动结束  "+num+" 条活动================");

		num = payMallInfoService.cronJobsUpdateEnable();
		logger.info("=============本次自动下架 "+num+" 款商品================");
		
		logger.info("---------处理过期订单------开始-----");
		List<PayOrder> orderList = payOrderService.selectOutTimeList();
		if(orderList != null && orderList.size()>0)
    	{
			logger.info("---------过期订单 "+orderList.size()+" 条-----------");
    		for(int i=0;i<orderList.size();i++)
    		{
    			PayOrder payOrder = (PayOrder)orderList.get(i);
    			logger.info("---------订单号："+payOrder.getOrderCode()+"-----------");
    			
    			PayBankroll payBankroll = payBankrollService.selectDetailByOrderCode(payOrder.getOrderCode());
				if(payBankroll != null && "0".equals(payBankroll.getState()))
				{
					payBankroll.setDelFlag("1");
					payBankrollService.updateById(payBankroll);
				}
				
				PayMallInfo payMallInfo = payMallInfoService.getById(payOrder.getMallId());
				if(payMallInfo != null)
				{
					logger.info("---------订单相关商品加库存      原库存"+payMallInfo.getStock()+"-----------");
					logger.info("---------订单相关商品减销量      原销量"+payMallInfo.getVolume()+"-----------");
					payMallInfo.setStock(payMallInfo.getStock()+payOrder.getMallNum());//加库存
					payMallInfo.setVolume(payMallInfo.getVolume()-payOrder.getMallNum());//减销量
					logger.info("---------订单相关商品加库存      现库存"+payMallInfo.getStock()+"-----------");
					logger.info("---------订单相关商品减销量      现销量"+payMallInfo.getVolume()+"-----------");
					payMallInfoService.updateById(payMallInfo);
					
					payOrder.setState("3");
					payOrderService.updateById(payOrder);	
					logger.info("---------订单号："+payOrder.getOrderCode()+"-状态更新为“已取消”----------");
				}
    		}
    	}
		else
			logger.info("---------无过期订单-----------");
		logger.info("---------处理过期订单------结束-----");
		logger.info("=============自动定时任务   结束================");
	}
	

}
